﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DatabaseComponent;

public partial class FaqAR : System.Web.UI.Page
{

    public List<Faqs> lstFaqsGrid
    {
        get
        {
            if (ViewState["lstFaqsGrid"] == null)
                ViewState["lstFaqsGrid"] = System.Guid.NewGuid().ToString();
            return Session[ViewState["lstFaqsGrid"].ToString()] as List<Faqs>;
        }
        set
        {
            if (ViewState["lstFaqsGrid"] == null)
                ViewState["lstFaqsGrid"] = System.Guid.NewGuid().ToString();
            Session[ViewState["lstFaqsGrid"].ToString()] = value;
        }
    }



    public Persons mobjPerson
    {
        get
        {
            if (ViewState["mobjPerson"] == null)
                ViewState["mobjPerson"] = System.Guid.NewGuid().ToString();
            return Session[ViewState["mobjPerson"].ToString()] as Persons;
        }

        set
        {
            if (ViewState["mobjPerson"] == null)
                ViewState["mobjPerson"] = System.Guid.NewGuid().ToString();
           Session[ViewState["mobjPerson"].ToString()] = value;
        }

    }



    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            checkSecurityAndCustomizePage();

            BindGrid("");
        }
    }


    /// <summary>
    /// this function will check the security of the current page
    /// </summary>
    private void checkSecurityAndCustomizePage()
    {
        //mobjPerson = GlobalVars.getCurrentPerson();

        //if (!mobjPerson.lstAllRulesIDs.Contains((long)RulesEnum.Ticket_add_page))
        //{
        //    Response.Redirect("LoginEN.aspx");
        //}
        
    }


    protected void BindGrid(string strSearch)
    {
        lstFaqsGrid = search(strSearch);
        grdFaq.DataSource = lstFaqsGrid;
        grdFaq.DataBind();

    }



    /// <summary>
    /// this function will search in Faq and return result
    /// </summary>
    /// <param name="txtText">the text to search for</param>
    /// <returns>a list of Faq found</returns>
    protected List<Faqs> search(string txtText)
    {



        FaqsDB objFaqsDB = new FaqsDB(ConnectionStrings.LMS);
        List<Faqs> lstRet = new List<Faqs>();

        if (txtText.Trim() == "")
        {

            List<Faqs> lstNew = objFaqsDB.Select("  ", " order by [FaqQuestionEN] ASC ");
            //lstNew = new List<Persons>(from objFol in lstNew where objFol.StatusID != 2 select objFol);
            return lstNew;
        }


        string[] strValues = null;
        if (txtText.Trim()[0] == '\"' && txtText.Trim()[txtText.Trim().Length - 1] == '\"')
        {
            try
            {
                strValues = new string[] { txtText.Trim().Substring(1, txtText.Trim().Length - 2) };
                if (strValues[0] == "")
                    strValues = new string[0];
            }
            catch
            {
                strValues = new string[0];
            }
        }
        else
        {
            strValues = txtText.Trim().Split(" ".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
        }



        foreach (string strValue1 in strValues)
        {
            string strValue = strValue1;
            strValue = strValue.Replace("'", "''");
            strValue = "%" + strValue + "%";

            string strWhere = " where [FaqQuestionEN] LIKE '" + strValue + "' " +
                                " OR [FaqQuestionAR] LIKE '" +  strValue + "' " +
                                " OR [FaqAnswerEN] LIKE '" + strValue + "' " +
                                " OR [FaqAnswerAR] LIKE '" + strValue + "' " +
                                " order by [FaqQuestionEN] ASC  ";


            List<Faqs> lstFaqs = objFaqsDB.Select("  ", strWhere);

            //lstPersons = new List<Persons>(from objFol in lstPersons where objFol.StatusID != 2 select objFol);

            foreach (Faqs objFol in lstFaqs)
            {
                if ((from objIn in lstRet where objIn.ID == objFol.ID select objIn).ToList().Count == 0)
                {
                    lstRet.Add(objFol);
                }
            }

        }


        return lstRet;



    }





    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindGrid(txtSearch.Text);
    }

    protected void grdFaq_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grdFaq.DataSource = lstFaqsGrid;
        grdFaq.PageIndex = e.NewPageIndex;
        grdFaq.DataBind();
    }
}